import http from '../utils/Http'
import validate from '../utils/Validate'

//接口API统一
export default class api {
    //获取产品分类，默认获取6个
    static apiProductTypes(limit) {
      limit = validate.optional(limit) ?limit:6
      return http.post('get_producttype&limit='+limit).then(resp => resp)
    }
    //获取幻灯片广告
    static apiBanner() {
      return http.post('get_advertisement_banner').then(resp => resp)
    }
    //根据id获取广告 ：id 5	中部长图 ，id 6 中部短图（左），id 7 中部短图
    static apiGetAdvertisementById (id) {
      return http.post('get_advertisement_by_id&id='+id).then(resp => resp)
    }
    //获取产品系列，默认获取6个
    static apiBrand(limit) {
      limit = validate.optional(limit) ?limit:100
      return http.post('get_brand&limit='+limit).then(resp => resp)
    }
    //获取产品系列下的产品，默认获取6个
    static apiGetProductByBrand (brandId,pageno) {
      let params = "";
      if(validate.optional(brandId)) params += '&brand_id='+brandId;
      if(validate.optional(pageno)) params += '&pageno='+pageno;
      else params += '&pageno='+1
      return http.post('get_product_by_brand'+params).then(resp => resp)
    }
    //根据产品分类id获取当前分类下面的所有
    static apiGetProductByType(typeId,pageno) {
      let params = "";
      if(validate.optional(typeId)) params += '&typeid='+typeId;
      if(validate.optional(pageno)) params += '&pageno='+pageno;
      else params += '&pageno='+1
      return http.post('get_product_by_typeid'+params).then(resp => resp)
    }
    //根据产品id获取产品详细信息
    static apiGetProductById(id) {
      return http.post('get_product_by_id&id='+id).then(resp => resp)
    }
    //根据产品分类获取当前分类下所有的系列
    static apiGetSeriesByProductType (typeId) {
      return http.post('get_series_by_product_type&typeid='+typeId).then(resp => resp)
    }
    //根据产品分类,产品系列。产品类型，产品性别 获取当前所有
    static apiGetProductByseriesBytypeBysexBytypeid(typeId,brandId,type,sex) {
      let params = '';
      if(validate.optional(typeId)) params += '&typeid='+typeId;
      if(validate.optional(brandId)) params += '&brandid='+brandId;
      if(validate.optional(type)) params += '&type='+type;
      if(validate.optional(sex)) params += '&sex='+sex;
      return http.post('get_product_byseries_bytype_bysex_bytypeid'+params).then(resp => resp)
    }
    //获取限时购买商品
    static apiLimitSaleProduct() {
      return http.post('limitsale_product').then(resp => resp)
    }
    //根据产品id获取参数
    static apiGetProductPamaValueById(id,limit) {
      let params = '';
      if(validate.optional(id)) params += '&product_id='+id;
      if(validate.optional(limit)) params += '&limit='+limit;
      else params += '&limit='+1;
      return http.post('get_product_pama_value_by_id'+params).then(resp => resp)
    }
    //根据产品id获取总库存
    static apiGetSumnumByProductid(id) {
      return http.post('get_sumnum_by_productid&good_id='+id).then(resp => resp)
    }
    //根据产品及选择的颜色或者尺寸获得库存数
    static apiGetProductNumByPama(pama,good_id) {
      return http.post('get_product_num_by_pama&pama='+pama+'&good_id='+good_id).then(resp => resp)
    }
    //获取短信验证码(实际就是发送短信验证码)
    static apiGetSmsCode(phone) {
      return http.post('get_sms_code&phone='+phone).then(resp => resp)
    }
    //用户注册，默认会注册用户并且随机产生一个token
    static apiUserReg(phone,sms_code,nick,imgUrl,sex,code) {
      let params = {};
      if(validate.optional(phone)) params.phone=phone;
      if(validate.optional(sms_code)) params.sms_code=sms_code;
      if(validate.optional(nick)) params.nick=nick;
      if(validate.optional(imgUrl)) params.imgUrl=imgUrl;
      if(validate.optional(sex)) params.sex=sex;
      if(validate.optional(code)) params.code=code;
      return http.post('user_reg',params).then(resp => resp)
    }
    //获取用户数据
    static apiGetUserInfo() {
      return http.post('get_user_info').then(resp => resp)
    }
    //获取用户收藏
    static apiGetUserFavorite() {
      return http.post('get_user_favorite').then(resp => resp)
    }
    //取消收藏（id是产品id）
    static apiDelToUserFavorite(goodId) {
      return http.post('del_to_user_favorite&good_id='+goodId).then(resp => resp)
    }
    //根据产品id新增用户收藏
    static apiInsertToUserFavorite(goodId) {
      return http.post('insert_to_user_favorite&good_id='+goodId).then(resp => resp)
    }
    //根据产品id返回用户收藏信息(判断是否已收藏)
    static apiGetUserFavoriteByProductid(goodId) {
      return http.post('get_user_favorite_by_productid&good_id='+goodId).then(resp => resp)
    }
    //新增地址（参数见文档）
    static apiInsertToUserAddress(address) {
      return http.post('insert_to_user_address',address).then(resp => resp)
    }
    //根据ID查询地址详情
    static apiQueryToUserAddressById(id) {
      return http.post('query_to_user_address_by_id&id='+id).then(resp => resp)
    }
    //查询用户下所有可用地址
    static apiQueryToUserAddress() {
      return http.post('query_to_user_address').then(resp => resp)
    }
    //快捷将地址设为默认
    static apiSetDefaultUserAddress(id) {
      return http.post('set_default_user_address&id='+id).then(resp => resp)
    }
    //更新购物地址(参数见文档)
    static apiModifyToUserAddress(address) {
      return http.post('modify_to_user_address',address).then(resp => resp)
    }
    //删除会员地址，要求字符串1,2,3  还得以逗号隔开
    static apiDelToUserAddress(addressIds) {
      return http.post('del_to_user_address&users_address_id='+addressIds).then(resp => resp)
    }
    //添加商品到购物车
    static apiInsertToShopcart(goodSkuId,goodId,num,price) {
      let params = {};
      if(validate.optional(goodSkuId)) params.good_sku_id = goodSkuId;
      if(validate.optional(goodId)) params.good_id = goodId;
      if(validate.optional(num)) params.num = num;
      if(validate.optional(price)) params.price = price;
      return http.post('insert_to_shopcart',params).then(resp => resp)
    }
    //删除购物车接口，要求字符串1,2,3  还得以逗号隔开
    static apiDelToShopcart(shopcartIds) {
      return http.post('del_to_shopcart&shopcart_id='+shopcartIds).then(resp => resp)
    }
    //修改商品数量到到购物车
    static apiAddminusToShopcart(shopcartId,num) {
      let params = '';
      if(validate.optional(shopcartId)) params += '&shopcart_id='+shopcartId;
      if(validate.optional(num)) params += '&num='+num;
      return http.post('addminus_to_shopcart'+params).then(resp => resp)
    }
    //查询用户当前购物车产品
    static apiQueryToShopcart() {
      return http.post('query_to_shopcart').then(resp => resp)
    }
    //用户关键字搜索
    static apiUserSearch(keyword) {
      return http.post('user_search&keyword='+keyword).then(resp => resp)
    }
    //获取关键字搜索
    static apiGetHotSearchKeyword() {
      return http.post('get_hot_search_keyword').then(resp => resp)
    }
    //获取历史搜索记录
    static apiGetHistorySearchKeyword() {
      return http.post('get_history_search_keyword').then(resp => resp)
    }
    //新增用户建议
    static apiInsertUsersSuggest(content,phone) {
      let params = {}
      if(validate.optional(content)) params.content=content;
      if(validate.optional(phone)) params.phone=phone;
      return http.post('insert_users_suggest',params).then(resp => resp)
    }
    //更新用户信息
    static apiUpdateUserInfoNameSexDate(name,sex,birthday) {
      let params = {}
      if(validate.optional(name)) params.name=name;
      if(validate.optional(sex)) params.sex=sex;
      if(validate.optional(birthday)) params.birthday=birthday;
      return http.post('update_user_info_name_sex_date',params).then(resp => resp)
    }
    //根据产品id返回提问
    static apiGetProductAskById(id) {
      return http.post('get_product_ask_by_id&product_id='+id).then(resp => resp)
    }
    //根据提问id 点赞
    static apiUpdateProductAskPoint(id,rate) {
      return http.post('update_product_ask_point&ask_id='+id+'&rate='+rate).then(resp => resp)
    }
     //根据评价id 点赞
    static apiUpdateProductCommentPoint(id,rate) {
      return http.post('update_product_comment_point&id='+id).then(resp => resp)
    }
    //回复某个帖子
    static apiReplyProductAsk(replayAsk) {
      let params = {}
      if(validate.optional(replayAsk.replyUsername)) params.reply_username=replayAsk.replyUsername;
      if(validate.optional(replayAsk.replyUserId)) params.reply_userid=replayAsk.replyUserId;
      if(validate.optional(replayAsk.replyAskId)) params.reply_ask_id=replayAsk.replyAskId;
      if(validate.optional(replayAsk.username)) params.username=replayAsk.username;
      if(validate.optional(replayAsk.userId)) params.user_id=replayAsk.userId;
      if(validate.optional(replayAsk.content)) params.content=replayAsk.content;
      return http.post('reply_product_ask',params).then(resp => resp)
    }
    //发表提问
    static apiSubmitProductAsk(productId,content) {
      let params = {}
      if(validate.optional(productId)) params.product_id=productId;
      if(validate.optional(content)) params.content=content;
      return http.post('submit_product_ask',params).then(resp => resp)
    }
    //获取评论
    static apiGetCommentsByProductId(productId,limit) {
      let params = '';
      if(validate.optional(productId)) params += '&product_id='+productId;
      if(validate.optional(limit)) params += '&limit='+limit;
      return http.post('get_comments_by_product_id'+params).then(resp => resp)
    }
    //发布评论
    static apiSubmitProductComments(orderId,content,imgList,isAnonymous) {
      let params = {}
      // if(validate.optional(productId)) params.product_id=productId;
      if(validate.optional(content)) params.content=content;
      if(validate.optional(imgList)) params.img_list=imgList;
      if(validate.optional(isAnonymous)) params.is_anonymous=isAnonymous;
      if(validate.optional(orderId)) params.order_id=orderId;
      return http.post('submit_product_comments',params).then(resp => resp)
    }
    //查询当前用户默认地址
    static apiQueryToUserDefaultAddress() {
      return http.post('query_to_user_default_address').then(resp => resp)
    }
    //根据shop_id获取店员
    static apiGetShopManagerByShopid(shopId) {
      return http.post('get_shop_manager_by_shopid&shop_id='+shopId).then(resp => resp)
    }
    //显示单个订单接口
    static apiOrderProductBySkuidSingle(skuId,num,addressId) {
      let params = '';
      if(validate.optional(skuId)) params += '&good_sku_id='+skuId;
      if(validate.optional(num)) params += '&num='+num;
      if(validate.optional(addressId)) params += '&address_id='+addressId;
      return http.post('order_product_by_skuid_single'+params).then(resp => resp)
    }
    //根据id调出订单明细
    static apiFindOrder(orderId) {
      return http.post('find_order&order_id='+orderId).then(resp => resp)
    }
    //提交订单结算
    static apiPayment(params) {
      return http.post('payment',params).then(resp => resp)  
    }
    //根据状态获取订单
    static apiGetUserOrder(order_status) {
      return http.post('get_user_order&order_status='+order_status).then(resp => resp) 
    }
    //确认订单收货
    static apiConfirmOrder(id) {
      return http.post('confirm_order&id='+id).then(resp => resp)
    }
    //取消订单
    static apiCancelOrder(id) {
      return http.post('cancel_order&id='+id).then(resp => resp)
    }
    //获取用户信息
    static apiGetOpenId(code,nick,imgUrl,sex) {
      let params = {}
      if(validate.optional(code)) params.code=code;
      
      return http.post('get_openId',params).then(resp => resp)
    }
    //获取微信用户权限
    static apiGetUserQuanxian () {
      return http.post('get_user_quanxian').then(resp => resp)
    }
    //微信预下单接口
    static apiWeixinPayment(price,orderId) {
      let params = '';
      if(validate.optional(price)) params += '&sum_pay_money='+price;
      if(validate.optional(orderId)) params += '&trade_order_id='+orderId;
      return http.post('weixin_payment'+params).then(resp => resp)
    }
    //获取用户视频
    static apiGetUserVideo() {
      return http.post('get_user_video').then(resp => resp)
    }

    static apiupload_fileTest(imgFile) {
      return http.post('upload_file&imgFile='+imgFile).then(resp => resp)
    }
    //获取用户地理位置坐标
    static apiGetReallyAddressByLatLan (latitude,longitude) {
      let params = {}
      if(validate.optional(latitude)) params.lat=latitude;
      if(validate.optional(longitude)) params.len=longitude;
      return http.post('get_really_address_by_lat_len',params).then(resp => resp)
    }
    //上传文件  
    static uploadFile(url, filePath, name, formData, cb) {  
      console.log('a=' + filePath)  
      wx.uploadFile({  
        url: http.baseUrl + url,  
        filePath: filePath,  
        name: name,  
        header: http.createAuthHeader(), 
        formData: formData, // HTTP 请求中其他额外的 form data  
        success: function (res) {  
          if (res.statusCode == 200 && !res.data.result_code) {  
            return typeof cb == "function" && cb(res.data)  
          } else {  
            return typeof cb == "function" && cb(false)  
          }  
        },  
        fail: function () {  
          return typeof cb == "function" && cb(false)  
        }  
      })  
    } 



}
